;2008-05-20
;Av Benjamin Ingberg och Patrik Johansson
;
;Huvudfilen, här schemerman körs, och denna som är toppen i arvsystemt

(load "gui-animation.scm")
(load "init-schemerman-gui.scm")
(load "key-listener.scm")
(load "test-level.scm")
(load "levels.scm")

(define *board* (new test-level%))

(define levels (list level1%
                     level2%
                     level3%))

(define counter 0)
(define win-counter -1)
(define won-game #f)
(define game-over #t)
(define win-lose-string "")

(define (check-win)
  (set! game-over #t)
  (cond ((send *board* won?) (set! win-lose-string "You've won the map! ")
                             (set! won-game #t))
        ((send *board* lost?) (set! win-lose-string "You've lost the game... ")
                              (set! won-game #f))
        (else (set! game-over #f))))

(define (next-game)
  (if won-game
      (if (not (null? (cdr levels)))
          (begin (set! *board* (new (car (cdr levels))))
                 (set! levels (cdr levels)))
          (exit))
      (set! *board* (new (car levels))))
  (set! win-counter 600)
  (set! game-over #f))

(define (update)
  (send *board* update)
  (if game-over
      (set! win-counter (- win-counter 1))
      (check-win))
  (if (< win-counter 0)
      (next-game)))

(define (draw)
  (clear)
  (send *board* draw)
  (set! counter (+ counter 1))
  (if game-over
      (draw-text (string-append win-lose-string (number->string win-counter))
                 10 10 *red-pen* *red-brush*))
  (show))

(define (play-game)
  (start-loop))

(play-game)